var app = getApp();
const db = wx.cloud.database();
Page({
  data: {
    isShowAuth:false,
    userInfo: {},
    ne: [],
  },
  onLoad: function () {

    var that = this;
    var roleId = ["xiaoyu00", "lyking90", "unknow","666","kekeke","star","test"];
    var admin = false;
    var re = false;
    db.collection('userInfo').get({
      success: res => {
        console.log("mine-onLoad执行，用户信息为")
        console.log(res)
        this.setData({
          ne: res.data
        })
      }
    })
    for (let i = 0; i < roleId.length; i++){
      db.collection('userInfo').where({
        userID: roleId[i] //进行筛选
      }).get({
        success:res=>{
          if (res.data.length === 1) {
            that.setData({
              admin: true
            })
          }
        }
      })
    }
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              wx.cloud.callFunction({    //调用云函数获取openid
                name:"getOpenId",
                complete:res=>{
                  db.collection("userInfo").where({
                    _openid: res.result.openid //进行筛选
                  }).get({
                    success:res=>{
                      if(res.data.length === 1){
                        that.setData({
                          re: true
                        })
                        wx.navigateTo({
                          url: '/pages/mine/mine',
                        })
                      }else {
                        that.setData({
                          re: false
                        })
                      }
                    }
                  })
                }
              });
            }
          });
        }else{
          wx.redirectTo({
            url: '../mine/login'
          });
        }
      }
    })
  },

  onShow:function(){
    db.collection('userInfo').get({
      success: res => {
       
        console.log("mine-onShow执行",res)
        this.setData({
          ne: res.data
        })
      }
    })
  },
  goRegister(e) {
    wx.navigateTo({
      url: '/pages/register/newuser'
    })
  },
  goMyTask(e) {
    wx.navigateTo({
      url: '/pages/myTask/myTask'
    })
  },
  goMyExchange(e) {
    wx.navigateTo({
      url: '/pages/myExchange/myExchange'
    })
  },
  goChangePro(e) {
    wx.navigateTo({
      url: '/pages/mine/changepro'
    })
  },
  goElsePro(e) {
    wx.navigateTo({
      url: '/pages/mine/elsePro'
    })
  },
  goAbout(e) {
    wx.navigateTo({
      url: '/pages/mine/about'
    })
  },
  goLogin(e) {
    wx.navigateTo({
      url: '/pages/mine/auth'
    })
  },
  goAdmin(e) {
    wx.navigateTo({
      url: '/pages/admin/admin'
    })
  },
  goSetting(e) {
    wx.navigateTo({
      url: '/pages/setting/setting'
    })
  },
  //下拉刷新
  onPullDownRefresh() {
    wx.showNavigationBarLoading({
      complete: (res) => {
        wx.showToast({
          title: '数据刷新中',
          mask: true,
          icon: "loading",
          duration: 2000
        })
        this.onLoad();
        wx.hideNavigationBarLoading({
          complete: (res) => {
            wx.stopPullDownRefresh({
            },3000)
          },
        })
      },
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
})